package socket;


import dao.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class HelloClient {
     private static final Logger logger = LoggerFactory.getLogger(HelloClient.class);

     public void send (String host, Integer port, String message){

         try {
             // 1、创建socket对象, 指定地址和端口
             Socket socket = new Socket(host, port);
             // 2、通过输出流向服务发出信息
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
             objectOutputStream.writeObject(new Message(message));
             // 3、接收服务响应的信息
             ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
             Message content = (Message) objectInputStream.readObject();
             logger.info("server response: " + content);
         } catch (IOException | ClassNotFoundException e) {
             logger.error("occur IOException", e);
         }
     }

    public static void main(String[] args) {
        HelloClient helloClient = new HelloClient();
        for (int i = 0; i < 100000; i++) {
            helloClient.send("127.0.0.1", 6666, "I am client" + i);
        }
    }
}
